﻿using System;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using vbAccelerator.Components.Shell;

namespace SansaImageConverter
{
    public partial class SettingsForm : Form
    {
		#region Constructors (1) 

        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsForm"/> class.
        /// </summary>
        public SettingsForm()
        {
            InitializeComponent();
        }

		#endregion Constructors 

		#region Event Handlers (5) 

        /// <summary>
        /// Handles the Click event of the btnBrowse control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            folderDialog.SelectedPath = txtPath.Text;
            if (folderDialog.ShowDialog() == DialogResult.Cancel) return;
            if (Directory.Exists(folderDialog.SelectedPath))
                txtPath.Text = folderDialog.SelectedPath;
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            switch (cboQuality.SelectedIndex)
            {
                case 0: Program.Settings.Quality = InterpolationMode.HighQualityBicubic; break;
                case 1: Program.Settings.Quality = InterpolationMode.High; break;
                case 2: Program.Settings.Quality = InterpolationMode.Default; break;
                case 3: Program.Settings.Quality = InterpolationMode.Low; break;
            }

            Program.Settings.Sizing = cboSizing.SelectedIndex;
            Program.Settings.PlayerPath = (string)cboDrive.SelectedItem;
            Program.Settings.RotateImages = chkRotate.Checked;
            Program.Settings.CopyToPlayer = chkCopyTo.Checked;
            Program.Settings.ExportPath = txtPath.Text;
            Program.Settings.Save();

            this.Close();
        }

        /// <summary>
        /// Handles the CheckedChanged event of the chkCopyTo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void chkCopyTo_CheckedChanged(object sender, EventArgs e)
        {
            cboDrive.Enabled = chkCopyTo.Checked;
            txtPath.Enabled = !chkCopyTo.Checked;
        }

        /// <summary>
        /// Handles the Load event of the SettingsForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SettingsForm_Load(object sender, EventArgs e)
        {
            SendToLinkCheck();

            txtPath.Text = Program.Settings.ExportPath;
            chkRotate.Checked = Program.Settings.RotateImages;
            chkCopyTo.Checked = Program.Settings.CopyToPlayer;
            cboSizing.SelectedIndex = Program.Settings.Sizing;
            
            switch (Program.Settings.Quality)
            {
                case InterpolationMode.Default: cboQuality.SelectedIndex = 2; break;
                case InterpolationMode.High: cboQuality.SelectedIndex = 1; break;
                case InterpolationMode.HighQualityBicubic: cboQuality.SelectedIndex = 0; break;
                case InterpolationMode.Low: cboQuality.SelectedIndex = 3; break;
                default: cboQuality.SelectedIndex = 0; break;
            }

            foreach (DriveInfo info in DriveInfo.GetDrives())
            {
                cboDrive.Items.Add(info.Name);
            }
            try
            {
                cboDrive.SelectedItem = Program.Settings.PlayerPath;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                cboDrive.SelectedIndex = 0;
            }
        }

        private void btnSendTo_Click(object sender, EventArgs e)
        {
            string sendToPath = Environment.GetFolderPath(Environment.SpecialFolder.SendTo);
            string linkPath = Path.Combine(sendToPath, Application.ProductName + ".lnk");

            using (ShellLink shortcut = new ShellLink())
            {
                shortcut.Target = Application.ExecutablePath;
                shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
                shortcut.Description = Application.ProductName;
                shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
                shortcut.Save(linkPath);
            }
            SendToLinkCheck();
        }

		#endregion Event Handlers 

        private void SendToLinkCheck()
        {
            string sendToPath = Environment.GetFolderPath(Environment.SpecialFolder.SendTo);
            string linkPath = Path.Combine(sendToPath, Application.ProductName + ".lnk");
            if (File.Exists(linkPath))
            {
                btnSendTo.Enabled = false;
            }
            else
            {
                btnSendTo.Enabled = true;
            }
        }

    }
}
